package xiaobange.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xiaobange.common.CustomException;
import xiaobange.dto.DishDto;
import xiaobange.entity.Dish;
import xiaobange.entity.DishFlavor;
import xiaobange.mapper.DishFlavorMapper;
import xiaobange.mapper.DishMapper;
import xiaobange.service.DishFlavorService;
import xiaobange.service.DishService;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImp extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品，同时添加口味
     * @param dishDto
     */
    @Transactional//开启事物mo
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //添加菜品到菜品表
        this.save(dishDto);
        //给口味实体类的ID附上值
        for (DishFlavor flavor : dishDto.getFlavors()) {
            flavor.setDishId(dishDto.getId());
        }
        //添加菜品的口味
        dishFlavorService.saveBatch(dishDto.getFlavors());  //saveBatch是批量保存
    }


    /**
     * 批量删除
     * @param ids
     */
    @Transactional
    @Override
    public void DeleteIds(Long[] ids) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(Dish::getId,ids);
        dishLambdaQueryWrapper.eq(Dish::getStatus,1);
        int count = this.count(dishLambdaQueryWrapper);
        if (count > 0){
            throw new CustomException("当前删除的菜品中包含正在上架销售，请删除后再进行删除！");
        }
        //批量删除菜品表里的菜品
        this.removeByIds(Arrays.asList(ids));
        //批量删除口味表里的数据
        //dishFlavorMapper.DishFlavorDeletes(ids);
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(lambdaQueryWrapper);
    }


    /**
     * 修改菜品状态
     * @param Status
     * @param ids
     */
    @Override
    public void UpdateStatus(int Status, Long[] ids) {
//        dishMapper.BatchStart(Status,ids);
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Dish::getId,ids);
        List<Dish> list = this.list(lambdaQueryWrapper);
        list = list.stream().map((item) ->{
            item.setStatus(Status);
            return item;
        }).collect(Collectors.toList());

        this.updateBatchById(list);
    }


    /**
     * 回显修改菜品信息
     * @param id
     */
    @Override
    public DishDto UpdateDataShow(Long id) {
        //查询菜品信息
        Dish dish = this.getById(id);
        //对象拷贝
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        //查询菜品口味信息
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> list = dishFlavorService.list(lambdaQueryWrapper);
        dishDto.setFlavors(list);
        return dishDto;
    }

    @Transactional
    @Override
    public void UpdateSubmit(DishDto dishDto) {
        //修改菜品信息
        this.updateById(dishDto);
        //修改菜品口味信息
        //先把原来的口味删除掉
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(lambdaQueryWrapper);
        //再给当前菜品添加修改后的口味
        //给当前口味添加对应的菜品ID
        /*List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) ->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);*/

        for (DishFlavor flavor : dishDto.getFlavors()) {
            flavor.setDishId(dishDto.getId());
        }
        dishFlavorService.saveBatch(dishDto.getFlavors());
    }


}
